package net.ib.mtalk.util;

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.util.DisplayMetrics;
import android.view.Display;

public class BaseGraphicUtil {
	
	public static int DipToPixel(Activity activity, int dip) {
		return (int)Dip.toPixel(dip);
		/*DisplayMetrics metrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
		return (int) (dip * metrics.density / 1.60);*/
	}	
	
	public static int getscrOrientation(Activity activity) {
		Display getOrient = activity.getWindowManager().getDefaultDisplay();
		int orientation = getOrient.getOrientation();
		if(orientation==Configuration.ORIENTATION_UNDEFINED){
			Configuration config = activity.getResources().getConfiguration();
			orientation = config.orientation;
			if(orientation==Configuration.ORIENTATION_UNDEFINED){
				if(getOrient.getWidth()==getOrient.getHeight()){
					orientation = Configuration.ORIENTATION_SQUARE;}
				else{ 
					if(getOrient.getWidth() < getOrient.getHeight()){
						orientation = Configuration.ORIENTATION_PORTRAIT;
					}
					else{ 
						orientation = Configuration.ORIENTATION_LANDSCAPE;
					}
				}
			}
		}
		return orientation;
	}
	
	public static Bitmap getResizedBitmap(Bitmap bitmap, int maxLength) {
		Bitmap result = null;
		
		int originalWidth = bitmap.getWidth();
		int originalHeight = bitmap.getHeight();
		
		if(originalHeight >= originalWidth) {
			int scaledWidth = maxLength * originalWidth / originalHeight;
			result = Bitmap.createScaledBitmap(bitmap, scaledWidth, maxLength, true);
		}
		else {
			int scaledHeight = maxLength * originalHeight / originalWidth;
			result = Bitmap.createScaledBitmap(bitmap, maxLength, scaledHeight, true);
		}
		bitmap.recycle();
		return result;
	}
	
	public static Bitmap getResizedBitmap(int minLength, Bitmap bitmap) {
		int originalWidth = bitmap.getWidth();
		int originalHeight = bitmap.getHeight();
		
		if(originalHeight >= originalWidth) {
			int scaledHeight = minLength * originalHeight / originalWidth;
			return Bitmap.createScaledBitmap(bitmap, minLength, scaledHeight, true);
		}
		else {
			int scaledWidth = minLength * originalWidth / originalHeight;
			return Bitmap.createScaledBitmap(bitmap, scaledWidth, minLength, true);
		}
	}
	
	public static String getBase64FromBitmap(Bitmap bitmap) {
		
		return getBase64FromBitmap(bitmap, CompressFormat.JPEG);
	}
	
	public static String getBase64FromBitmap(Bitmap bitmap, CompressFormat format) {
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		bitmap.compress(format, 90, baos);
		
		String encodedImage = Base64.encode(baos.toByteArray());
		
		return encodedImage;
	}
	
	public static int getMajorAxis(Bitmap bitmap) {
		return (bitmap.getWidth() > bitmap.getHeight())? bitmap.getWidth() : bitmap.getHeight();
	}
	
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
		if(bitmap != null && roundPx > 0) {
			Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
			Canvas canvas = new Canvas(output);

			final int color = 0xff424242;
			final Paint paint = new Paint();
			final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
			final RectF rectF = new RectF(rect);

			paint.setAntiAlias(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(color);
			canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bitmap, rect, rect, paint);
			bitmap.recycle();
			return output;
		} else {
			return bitmap;
		}
	}
}
